package com.currency.mall.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.entity.UserAccount;
import com.currency.mall.entity.UserInfo;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.ui.my.BankCardActivity;
import com.currency.mall.ui.my.NameAutoActivity;
import com.currency.mall.ui.my.SettingActivity;
import com.currency.mall.ui.my.UserInfoActivity;
import com.currency.mall.ui.my.capital.CapitalListActivity;
import com.currency.mall.ui.my.cash.WithdrawalActivity;
import com.currency.mall.ui.my.recharge.RechargeActivity;
import com.currency.mall.ui.web.WebViewActivity;
import com.currency.mall.util.Util;
import com.mifengs.commonbase.base.BaseFragment;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/4.
 */

public class MyFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    Unbinder unbinder;
    @BindView(R.id.tv_authentication_status)
    TextView tvAuthenticationStatus;
    @BindView(R.id.rl_auth)
    RelativeLayout rlAuth;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_accout_balance)
    TextView tvAccoutBalance;
    @BindView(R.id.tv_available_price)
    TextView tvAvailablePrice;
    @BindView(R.id.tv_frozen_price)
    TextView tvFrozenPrice;
    private boolean isAuto = false;

    public MyFragment() {
    }

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, mView);
        StatusBarCompat.setStatusBarColor(getActivity(), ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(getActivity());
        init();
        return mView;
    }
    private void init(){
        if(swipeRefreshLayout !=null)
        swipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        if(PreferenceUtils.getLoginStatus())
            getUserCenter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_auth, R.id.rl_bind, R.id.rl_capital_flow, R.id.rl_help_center, R.id.rl_setting, R.id.ll_recharge, R.id.ll_withdrawal, R.id.ll_user})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.rl_auth:
                if (isAuto) {
                    intent = new Intent(getContext(), UserInfoActivity.class);
                    intent.putExtra("isAuto", isAuto);
                    startActivity(intent);
                } else {
                    startActivity(new Intent(getContext(), NameAutoActivity.class));
                }
                break;
            case R.id.rl_bind:
                if (isAuto) {
                    startActivity(new Intent(getContext(), BankCardActivity.class));
                }else{
                    startActivity(new Intent(getContext(), NameAutoActivity.class));
                    ToastUitl.show("添加银行卡之前，请先进行实名认证", 200);
                }
                break;
            case R.id.rl_capital_flow:
                startActivity(new Intent(getContext(), CapitalListActivity.class));
                break;
            case R.id.ll_recharge:
                startActivity(new Intent(getContext(), RechargeActivity.class));
                break;
            case R.id.ll_withdrawal:
                if (isAuto) {
                    startActivity(new Intent(getContext(), WithdrawalActivity.class));
                } else {
                    startActivity(new Intent(getContext(), NameAutoActivity.class));
                    ToastUitl.show("提现前，请先进行实名认证", 200);
                }
                break;
            case R.id.rl_help_center:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("ExtraWebPath", "http://150.109.125.51/help/index.html");
                intent.putExtra("ExtraWebTitle", "帮助中心");
                startActivity(intent);
                break;
            case R.id.rl_setting:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
            case R.id.ll_user:
                intent = new Intent(getContext(), UserInfoActivity.class);
                intent.putExtra("isAuto", isAuto);
                startActivity(intent);
                break;
        }
    }

    public void getUserCenter() {
        HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("userName",inputUser.getText().toString().trim() );
        NetWork.getUserApi().userAccount()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserAccount>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        swipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onNext(UserAccount userAccount) {
                        swipeRefreshLayout.setRefreshing(false);
                        if (userAccount.getCode().equals(Constant.SUCCESS_CODE)) {
                            tvAccoutBalance.setText(Util.formatTosepara(Double.parseDouble(userAccount.getTotalSum())));
                            tvAvailablePrice.setText(Util.formatTosepara(Double.parseDouble(userAccount.getUsabSum())));
                            tvFrozenPrice.setText(Util.formatTosepara(Double.parseDouble(userAccount.getFreezeSum())));
                            if ("false".equals(userAccount.getIdentity())) {
                                tvAuthenticationStatus.setText("立即认证");
                                isAuto = false;
                            } else {
                                tvAuthenticationStatus.setText("已认证");
                                isAuto = true;
                            }
                        } else {
//                            ToastUitl.show(userAccount.getMsg(), 200);
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        getUserCenter();
    }
}
